package com.padakeji.android.ui.examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.padakeji.android.ui.widget.PinnedHeaderListAdapter;
import com.padakeji.android.ui.widget.PinnedHeaderListView;

public class MainActivity extends Activity {
	
	private PinnedHeaderListView pinnedHeaderList;
	private PinnedHeaderListAdapter mAdapter;
	
	private LayoutInflater inflater;
	
	private LinkedHashMap<String, List<String>> datas;
	private List<String> titles;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		init();
		
		inflater = getLayoutInflater();
		
		pinnedHeaderList = (PinnedHeaderListView)findViewById(R.id.main_expandable);
		
		mAdapter = new PinnedHeaderListAdapter() {
			
			@Override
			public boolean isChildSelectable(int groupPosition, int childPosition) {
				return false;
			}
			
			@Override
			public boolean hasStableIds() {
				return true;
			}
			
			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,	View convertView, ViewGroup parent) {
				GroupHolder holder = null;
				if (convertView == null) {
					convertView = inflater.inflate(R.layout.item_main_group, null);
					holder = new GroupHolder();
					holder.groupText = (TextView) convertView.findViewById(R.id.main_text_group);
					convertView.setTag(holder);
				} else {
					holder = (GroupHolder) convertView.getTag();
				}
				
				holder.groupText.setText(titles.get(groupPosition));
				
				return convertView;
			}
			
			@Override
			public long getGroupId(int groupPosition) {
				return groupPosition;
			}
			
			@Override
			public int getGroupCount() {
				return titles.size();
			}
			
			@Override
			public Object getGroup(int groupPosition) {
				return titles.get(groupPosition);
			}
			
			@Override
			public int getChildrenCount(int groupPosition) {
				return datas.get(titles.get(groupPosition)).size();
			}
			
			@Override
			public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
				ChildHolder holder = null;
				if (convertView == null) {
					convertView = inflater.inflate(R.layout.item_main_child, null);
					holder = new ChildHolder();
					holder.childText = (TextView) convertView.findViewById(R.id.main_text_child);
					convertView.setTag(holder);
				} else {
					holder = (ChildHolder) convertView.getTag();
				}
				holder.childText.setText(getChild(groupPosition, childPosition).toString());
				return convertView;
			}
			
			@Override
			public long getChildId(int groupPosition, int childPosition) {
				return childPosition;
			}
			
			@Override
			public String getChild(int groupPosition, int childPosition) {
				return datas.get(titles.get(groupPosition)).get(childPosition);
			}
		};
		
		pinnedHeaderList.setAdapter(mAdapter);
		
		pinnedHeaderList.setOnGroupClickListener(new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				return false;
			}
		});
		
		TextView textView = new TextView(this);
		textView.setText("Head");
		textView.setTextSize(this.getResources().getDimension(R.dimen.acitivty_added_header_textsize));
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		textView.setLayoutParams(params);
		TextView textView1 = new TextView(this);
		textView1.setText("Head1");
		textView1.setTextSize(this.getResources().getDimension(R.dimen.acitivty_added_header_textsize));
		LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		textView1.setLayoutParams(params1);
		pinnedHeaderList.addHeaderView(textView);
		pinnedHeaderList.addHeaderView(textView1);
		
	}
	
	
	public static class GroupHolder {
		TextView groupText;
	}
	
	public static class ChildHolder {
		TextView childText;
	}

	private void init() {
		datas = new LinkedHashMap<String, List<String>>();
		datas.put("天空之城", Arrays.asList(new String[] {"1", "2"}));
		datas.put("爱的罗曼斯", Arrays.asList(new String[] {"1", "2"}));
		datas.put("献给爱丽丝", Arrays.asList(new String[] {"1", "2"}));
		datas.put("神秘花园", Arrays.asList(new String[] {"1", "2"}));
		datas.put("天空之城1", Arrays.asList(new String[] {"1", "2"}));
		datas.put("爱的罗曼斯1", Arrays.asList(new String[] {"1", "2"}));
		datas.put("献给爱丽丝1", Arrays.asList(new String[] {"1", "2"}));
		datas.put("神秘花园1", Arrays.asList(new String[] {"1", "2"}));
		datas.put("天空之城2", Arrays.asList(new String[] {"1", "2"}));
		datas.put("爱的罗曼斯2", Arrays.asList(new String[] {"1", "2"}));
		datas.put("献给爱丽丝2", Arrays.asList(new String[] {"1", "2"}));
		datas.put("神秘花园2", Arrays.asList(new String[] {"1", "2"}));
		datas.put("天空之城3", Arrays.asList(new String[] {"1", "2"}));
		datas.put("爱的罗曼斯3", Arrays.asList(new String[] {"1", "2"}));
		datas.put("献给爱丽丝3", Arrays.asList(new String[] {"1", "2"}));
		datas.put("神秘花园3", Arrays.asList(new String[] {"1", "2"}));
		datas.put("天空之城4", Arrays.asList(new String[] {"1", "2"}));
		datas.put("爱的罗曼斯4", Arrays.asList(new String[] {"1", "2"}));
		datas.put("献给爱丽丝4", Arrays.asList(new String[] {"1", "2"}));
		datas.put("神秘花园4", Arrays.asList(new String[] {"1", "2"}));
		titles = new ArrayList<String>(datas.keySet());
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
